Istotne aspekty przy realizowaniu projektu/zadania uczenia maszynowego:
Dane: $(X, y)$, gdzie $X$ to zmienne wyjaśniające a $y$ to zmienna wyjaśniana;
Cel: Znaleźć optymalną funkcję przekształcającą $X$ w $y$.
Dwa typy problemów: regresja (gdy zmienna wyjaśniana jest ciągła), klasyfikacja (gdy zmienna wyjaśniana jest dyskretna);
Typy klasyfikacji: binarna (binary), wieloklasowa (multiclass), wieloetykietowa (multi-label). Przykłady?

Dane: $X$ - zbiór danych bez zmiennej wyjaśnianej;
Cel: Znaleźć "ciekawą" strukturę w danych.
Przykłady?

Przykłady?




def loss_function(y_pred, y_real):
return 1/2 * np.mean((y_pred-y_real)**2)
def batch_gradient_descent(theta,
X,
y,
n_iteration,
alpha,
theta_snapshot_rate=None):
theta_snapshot_list = [[copy.deepcopy(theta), 0]]
for iteration in range(n_iteration):
theta = copy.deepcopy(theta)
if iteration % theta_snapshot_rate == 0 and iteration!=0:
append_list = [theta, iteration]
theta_snapshot_list.append(append_list)
gradient = np.mean(X.T.dot(X.dot(theta) - y), axis=1)
gradient = np.reshape(gradient, (-1, 1))
theta -= alpha * gradient
if len(theta_snapshot_list) > 0:
return theta, theta_snapshot_list
else:
return theta
PlotUtil.plot_gradient_descent(df, 'GrLivArea', 'SalePrice', thetas_snapshot, alpha, 0, 6000, 0, 1000000)
PlotUtil.plot_gradient_descent(df, 'GrLivArea', 'SalePrice', thetas_snapshot, alpha, 0, 6000, -1000000, 10000000)
PlotUtil.plot_gradient_descent(df, 'GrLivArea', 'SalePrice', thetas_snapshot, alpha, 0, 6000, 0, 1000000)
def learning_schedule(t, t0, t1):
return t0/(t+t1)
def stoch_gradient_descent(theta,
X,
y,
n_iteration,
alpha=None,
learning_schedule=None,
t0=None,
t1=None,
theta_snapshot_rate=None):
theta_snapshot_list = [[copy.deepcopy(theta), 0]]
for iteration in range(n_iteration):
theta = copy.deepcopy(theta)
if iteration % theta_snapshot_rate == 0 and iteration != 0:
append_list = [theta, iteration]
theta_snapshot_list.append(append_list)
for i in range(len(X)):
rand_idx = np.random.randint(len(X))
xi = X_b[rand_idx:rand_idx+1]
yi = y[rand_idx:rand_idx+1]
gradient = xi.T.dot(xi.dot(theta) - yi)
if learning_schedule:
alpha = learning_schedule(iteration*len(X)+i, t0, t1)
theta -= alpha*gradient
if len(theta_snapshot_list) > 0:
return theta, theta_snapshot_list
else:
return theta
n_iteration = 101
t0, t1 = 5, 5*10**8
theta_0 = np.zeros((2,1))
thetas_end, thetas_snapshot = stoch_gradient_descent(theta_0,
X_b,
y,
n_iteration,
learning_schedule=learning_schedule,
t0=t0,
t1=t1,
theta_snapshot_rate=1)
PlotUtil.plot_gradient_descent(df, 'GrLivArea', 'SalePrice', thetas_snapshot, (t0, t1), 0, 6000, 0, 1000000, "t0, t1:")
n_iteration = 101
alpha = 10**-10
theta_0 = np.zeros((2,1))
thetas_end, thetas_snapshot = stoch_gradient_descent(theta_0,
X_b,
y,
n_iteration,
alpha=alpha,
theta_snapshot_rate=1)
PlotUtil.plot_gradient_descent(df, 'GrLivArea', 'SalePrice', thetas_snapshot, alpha, 0, 6000, 0, 1000000)